package com.example.sangong.utils;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.util.Map;

@Component
@Slf4j
public class RedissonUtils {
    @Autowired
    private RedissonClient redissonClient;

    @SneakyThrows
    public void hset(String key, String field, String val) {
        RMap<String, String> map = redissonClient.getMap(key);
        map.put(field, val);
    }

    @SneakyThrows
    public void hdel(String key, String field) {
        RMap<String, String> map = redissonClient.getMap(key);
        map.remove(field);
    }

    @SneakyThrows
    public String hget(String key, String field) {
        String ip = InetAddress.getLocalHost().getHostAddress();
        RMap<String, String> map = redissonClient.getMap(key);
        return map.get(field);
    }

    @SneakyThrows
    public boolean isExists(String key) {
        RBucket<String> bucket = redissonClient.getBucket(key);
        return bucket.isExists();
    }

    @SneakyThrows
    public Map<String, String> hgetAll(String key) {
        RMap<String, String> map = redissonClient.getMap(key);
        return map.readAllMap();

    }
}
